/**
 * fileName:  FruitBean.java
 * author:  Lei
 * Company:  Cloudland Studio
 * Createdate:  2014-6-24 下午11:22:15
 *
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2011
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2014-6-24          Lei            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.commercial.masterdata.handler.support;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.cloudland.commercial.masterdata.bean.FruitArgument;
import org.cloudland.commercial.masterdata.bean.FruitEfficacy;
import org.cloudland.commercial.masterdata.bean.FruitInfo;
import org.cloudland.commercial.masterdata.bean.FruitNutrition;
import org.cloudland.commercial.masterdata.bean.FruitStructure;
import org.cloudland.commercial.masterdata.store.entity.ArgumentEntity;
import org.cloudland.commercial.masterdata.store.entity.CategoryEntity;
import org.cloudland.commercial.masterdata.store.entity.CostEntity;
import org.cloudland.commercial.masterdata.store.entity.FruitArgumentEntity;
import org.cloudland.commercial.masterdata.store.entity.FruitEfficacyEntity;
import org.cloudland.commercial.masterdata.store.entity.FruitEntity;
import org.cloudland.commercial.masterdata.store.entity.FruitNutritionEntity;
import org.cloudland.commercial.masterdata.store.entity.FruitStructureEntity;
import org.cloudland.commercial.masterdata.store.entity.NutritionEntity;
import org.cloudland.commercial.masterdata.store.read.ArgumentCostReader;
import org.cloudland.commercial.masterdata.store.read.ArgumentReader;
import org.cloudland.commercial.masterdata.store.read.EfficacyReader;
import org.cloudland.commercial.masterdata.store.read.FruitArgumentReader;
import org.cloudland.commercial.masterdata.store.read.FruitCategoryReader;
import org.cloudland.commercial.masterdata.store.read.FruitEfficacyRelationReader;
import org.cloudland.commercial.masterdata.store.read.FruitNutritionReader;
import org.cloudland.commercial.masterdata.store.read.FruitStructureReader;
import org.cloudland.commercial.masterdata.store.read.NutrientReader;
import org.cloudland.commercial.masterdata.store.write.FruitArgumentWriter;
import org.cloudland.commercial.masterdata.store.write.FruitEfficacyRelationWriter;
import org.cloudland.commercial.masterdata.store.write.FruitMasterWriter;
import org.cloudland.commercial.masterdata.store.write.FruitNutritionWriter;
import org.cloudland.commercial.masterdata.store.write.FruitStructureWriter;
import org.cloudland.util.Transforms;

 /**
 * <pre> 
 * TODO 请充分描述该类提供的作用, 或与其他类会有关联的使用。
 * 使用 java.lang.Object#toString(), 或者 {@link java.lang.Character Character} 等标签
 * </pre>
 * @ClassName  FruitBean
 * @author  Lei
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
public class FruitBean implements FruitInfo {
	
	/**
	 * fruitEntity 水果主数据实体
	 */
	private FruitEntity fruitEntity;
	
	/**
	 * efficacyEntity 营养集合
	 */
	private List<FruitNutritionEntity> nutritionArray = new ArrayList<FruitNutritionEntity>(10);
	
	/**
	 * argumentArray 动态参数集合
	 */
	private List<FruitArgumentEntity> argumentArray = new ArrayList<FruitArgumentEntity>(10);
	
	/**
	 * structureArray 水果结构信息集合
	 */
	private List<FruitStructureEntity> structureArray = new ArrayList<FruitStructureEntity>(6);
	
	/**
	 * fruitMasterWriter 主数据写操作
	 */
	private FruitMasterWriter fruitMasterWriter;
	
	/**
	 * fruitNutritionWriter 水果营养信息写入接口
	 */
	private FruitNutritionWriter fruitNutritionWriter;
	
	/**
	 * fruitArgumentWriter 水果动态参数写入接口
	 */
	private FruitArgumentWriter fruitArgumentWriter;
	
	/**
	 * fruitStructureWriter 水果结构信息写入接口
	 */
	private FruitStructureWriter fruitStructureWriter;
	
	/**
	 * fruitEfficacyRelationWriter 水果结构功效关系写入接口
	 */
	private FruitEfficacyRelationWriter fruitEfficacyRelationWriter;
	
	/**
	 * fruitNutritionReader 水果营养信息读取接口
	 */
	private FruitNutritionReader fruitNutritionReader;
	
	/**
	 * fruitNutritionReader 营养信息读取接口
	 */
	private NutrientReader nutritionReader;
	
	/**
	 * fruitArgumentReader 水果动态参数读取接口
	 */
	private FruitArgumentReader fruitArgumentReader;
	
	/**
	 * argumentReader 参数读取接口
	 */
	private ArgumentReader argumentReader;
	
	/**
	 * argumentCostReader 参数值读取接口接口
	 */
	private ArgumentCostReader argumentCostReader;
	
	/**
	 * fruitStructureReader 水果结构读取接口
	 */
	private FruitStructureReader fruitStructureReader;
	
	/**
	 * fruitEfficacyRelationReader 水果结构功效读取接口
	 */
	private FruitEfficacyRelationReader fruitEfficacyRelationReader;
	
	/**
	 * efficacyReader 功效读取接口
	 */
	private EfficacyReader efficacyReader;
	
	/**
	 * fruitCategoryReader 水果分类查询接口
	 */
	private FruitCategoryReader fruitCategoryReader;
	
	/**
	 * <pre>
	 * TODO 请充分描述该构造函数的作用，及其参数的作用。
	 * </pre>
	 *   
	 */
	public FruitBean(FruitMasterWriter fruitMasterWriter, 
			FruitNutritionWriter fruitNutritionWriter, 
			FruitArgumentWriter fruitArgumentWriter, 
			FruitStructureWriter fruitStructureWriter,
			FruitEfficacyRelationWriter fruitEfficacyRelationWriter,
			FruitNutritionReader fruitNutritionReader,
			NutrientReader nutritionReader,
			FruitArgumentReader fruitArgumentReader,
			ArgumentReader argumentReader,
			ArgumentCostReader argumentCostReader,
			FruitStructureReader fruitStructureReader,
			FruitEfficacyRelationReader fruitEfficacyRelationReader,
			EfficacyReader efficacyReader,
			FruitCategoryReader fruitCategoryReader) {
		this(new FruitEntity(), fruitMasterWriter, fruitNutritionWriter, fruitArgumentWriter, fruitStructureWriter, fruitEfficacyRelationWriter, fruitNutritionReader, nutritionReader, fruitArgumentReader, argumentReader, argumentCostReader, fruitStructureReader, fruitEfficacyRelationReader, efficacyReader, fruitCategoryReader);
	}

	/**
	 * <pre>
	 * TODO 请充分描述该构造函数的作用，及其参数的作用。
	 * </pre>
	 *   
	 */
	public FruitBean(FruitEntity fruitEntity, FruitMasterWriter fruitMasterWriter, 
			FruitNutritionWriter fruitNutritionWriter, 
			FruitArgumentWriter fruitArgumentWriter, 
			FruitStructureWriter fruitStructureWriter,
			FruitEfficacyRelationWriter fruitEfficacyRelationWriter,
			FruitNutritionReader fruitNutritionReader,
			NutrientReader nutritionReader,
			FruitArgumentReader fruitArgumentReader,
			ArgumentReader argumentReader,
			ArgumentCostReader argumentCostReader,
			FruitStructureReader fruitStructureReader,
			FruitEfficacyRelationReader fruitEfficacyRelationReader,
			EfficacyReader efficacyReader,
			FruitCategoryReader fruitCategoryReader) {
		this.fruitEntity = fruitEntity;
		this.fruitMasterWriter = fruitMasterWriter;
		this.fruitNutritionWriter = fruitNutritionWriter;
		this.fruitArgumentWriter = fruitArgumentWriter;
		this.fruitStructureWriter = fruitStructureWriter;
		this.fruitEfficacyRelationWriter = fruitEfficacyRelationWriter;
		this.fruitNutritionReader = fruitNutritionReader;
		this.nutritionReader = nutritionReader;
		this.fruitArgumentReader = fruitArgumentReader;
		this.argumentReader = argumentReader;
		this.argumentCostReader = argumentCostReader;
		this.fruitStructureReader = fruitStructureReader;
		this.fruitEfficacyRelationReader = fruitEfficacyRelationReader;
		this.efficacyReader = efficacyReader;
		this.fruitCategoryReader = fruitCategoryReader;
	}

	/**
	 * @return
	 */
	public String getId() {
		return fruitEntity.getFruitId();
	}

	/**
	 * @return
	 */
	public String getFruitName() {
		return fruitEntity.getFruitName();
	}

	/**
	 * @param fruitName
	 */
	public void setFruitName(String fruitName) {
		fruitEntity.setFruitName(fruitName);
		fruitEntity.setFruitPinyin(Transforms.doCn2Spell(fruitName));
	}
	
	/**
	 * @return
	 */
	public String getFruitNamePinyin() {
		return fruitEntity.getFruitPinyin();
	}

	/**
	 * @return
	 */
	public String getFruitAliasName() {
		return fruitEntity.getFruitAliasName();
	}

	/**
	 * @param fruitAliasName
	 */
	public void setFruitAliasName(String fruitAliasName) {
		fruitEntity.setFruitAliasName(fruitAliasName);
	}

	/**
	 * @return
	 */
	public String getFruitCategoryId() {
		return fruitEntity.getFruitCategoryId();
	}
	
	public String getFruitCategoryName() {
		CategoryEntity entity = fruitCategoryReader.findCategoryById(getFruitCategoryId());
		return entity.getName();
	}

	/**
	 * @param fruitCategoryId
	 */
	public void setFruitCategoryId(String fruitCategoryId) {
		fruitEntity.setFruitCategoryId(fruitCategoryId);
	}

	/**
	 * @return
	 */
	public String getCountryId() {
		return fruitEntity.getCountryId();
	}

	/**
	 * @param countryId
	 */
	public void setCountryId(String countryId) {
		fruitEntity.setCountryId(countryId);
	}

	/**
	 * @return
	 */
	public String getRegionId() {
		return fruitEntity.getRegionId();
	}

	/**
	 * @param regionId
	 */
	public void setRegionId(String regionId) {
		fruitEntity.setRegionId(regionId);
	}

	/**
	 * @return
	 */
	public String getDescription() {
		return fruitEntity.getDescription();
	}

	/**
	 * @param description
	 */
	public void setDescription(String description) {
		fruitEntity.setDescription(description);
	}

	/**
	 * @return
	 */
	public String getTaboo() {
		return fruitEntity.getTaboo();
	}

	/**
	 * @param taboo
	 */
	public void setTaboo(String taboo) {
		fruitEntity.setTaboo(taboo);
	}

	/**
	 * @param structure
	 */
	public void addStructure(FruitStructure structure) {
		FruitStructureEntity entity = new FruitStructureEntity(structure.getId(), fruitEntity.getFruitId(), structure.getName(), structure.getDescribe(), structure.getStead().ordinal());
		for (FruitEfficacy info : structure.getEfficacyInfo()) {
			entity.addEfficacy(new FruitEfficacyEntity(fruitEntity.getFruitId(), structure.getId(), info.getId(), info.getWeight(), info.getSort()));
		}
		structureArray.add(entity);
	}

	/**
	 * @return
	 */
	public List<FruitStructure> getStructure() {
		
		List<FruitEfficacyEntity> efficacyArray = null;
		if (null == structureArray || structureArray.size() <= 0) {
			// 获取所有结构
			structureArray = fruitStructureReader.findFruitStructureByFruitId(fruitEntity.getFruitId());
			// 获取所有结构的功效
			efficacyArray = fruitEfficacyRelationReader.findEfficacyRelationByFruitId(fruitEntity.getFruitId());
		}
		
		if (null == structureArray || structureArray.size() <= 0) {
			return new ArrayList<FruitStructure>(0);
		}
		
		HashMap<String, FruitStructureEntity> fruitStructure = new HashMap<String, FruitStructureEntity>();
		for (FruitStructureEntity entity : structureArray) {
			fruitStructure.put(entity.getSiteId(), entity);
		}
		
		for (FruitEfficacyEntity efficacy : efficacyArray) {
			fruitStructure.get(efficacy.getStructureId()).addEfficacy(efficacy);
		}
		
		List<FruitStructure> result = new ArrayList<FruitStructure>(fruitStructure.size());
		for (FruitStructureEntity entity : fruitStructure.values()) {
			result.add(new FruitStructureBean(entity, efficacyReader));
		} 
		
		return result;
	}

	/**
	 * @param argumentId
	 * @param costId
	 */
	public void addArgumentAndorCost(String argumentId, String... costId) {
		argumentArray.add(new FruitArgumentEntity(argumentId, costId));
	}

	/**
	 * @param argumentId
	 * @param customValue
	 */
	public void addArgumentAndorCustomCost(String argumentId, String customValue) {
		FruitArgumentEntity entity = new FruitArgumentEntity(argumentId);
		entity.setCostCustom(customValue);
		argumentArray.add(entity);
	}

	/**
	 * @return
	 */
	public List<FruitArgument> getArgumentInfo() {
		
		List<FruitArgumentEntity> fruitArgumentArray = fruitArgumentReader.FindArgumentRelationByFruitId(fruitEntity.getFruitId());
		
		if (null == fruitArgumentArray || fruitArgumentArray.size() <= 0) {
			return new ArrayList<FruitArgument>(0);
		}
		
		List<FruitArgument> result = new ArrayList<FruitArgument>(fruitArgumentArray.size());
		ArgumentEntity argumentEntity = null;
		List<ArgumentCostBean> argumentCostArray = null;
		CostEntity costEntity = null;
		for (FruitArgumentEntity relation : fruitArgumentArray) {
			// 获取动态参数信息
			argumentEntity = argumentReader.findArgumentById(relation.getArgumentId());
			
			// 获取值信息
			if (null == relation.getCostId() || relation.getCostId().length <= 0) {
				costEntity = new CostEntity();
				costEntity.setCostName(relation.getCostCustom());
				
				argumentCostArray = new ArrayList<ArgumentCostBean>(1);
				argumentCostArray.add(new ArgumentCostBean(costEntity, null, null));
			} else {
				argumentCostArray = new ArrayList<ArgumentCostBean>(relation.getCostId().length);
				for (String id : relation.getCostId()) {
					costEntity = argumentCostReader.findCostById(id);
					argumentCostArray.add(new ArgumentCostBean(costEntity, null, null));
				}
			}
			
			// 添加动态属性与值信息
			result.add(new FruitArgumentBean(new ArgumentBean(argumentEntity, null), argumentCostArray.toArray(new ArgumentCostBean[argumentCostArray.size()])));
		}
		
		return result;
	}

	/**
	 * @param nutritionInfo
	 */
	public void addNutrition(FruitNutrition nutritionInfo) {
		nutritionArray.add(new FruitNutritionEntity(fruitEntity.getFruitId(), nutritionInfo.getNutritionId(), nutritionInfo.getNutritionScalar(), nutritionInfo.getUnit(), nutritionInfo.getSort(), nutritionInfo.getNutritionType()));
	}

	/**
	 * @return
	 */
	public List<FruitNutrition> getNutrition() {
		
		if (null == nutritionArray || nutritionArray.size() <= 0) {
			nutritionArray =  fruitNutritionReader.findFruitNutritionById(fruitEntity.getFruitId());
		}
		
		List<FruitNutrition> result = new ArrayList<FruitNutrition>(nutritionArray.size());
		NutritionEntity nutritionEntity = null;
		for (FruitNutritionEntity entity : nutritionArray) {
			nutritionEntity = nutritionReader.findNutrientById(entity.getNutritionId());
			result.add(new FruitNutritionBean(entity, nutritionEntity));
		}
		
		return result;
	}
	
	public void submit() {
		
		// 保存水果主数据
		if (null != fruitEntity.getFruitId() && fruitEntity.getFruitId().length() > 0) {
			fruitMasterWriter.update(fruitEntity);
			
			fruitNutritionWriter.deleteByFruitId(fruitEntity.getFruitId());
			fruitArgumentWriter.deleteByFruitId(fruitEntity.getFruitId());
			fruitStructureWriter.deleteByFruitId(fruitEntity.getFruitId());
			fruitEfficacyRelationWriter.deleteByFruitId(fruitEntity.getFruitId());
		} else {
			fruitMasterWriter.save(fruitEntity);
		}
		
		// 保存水果营养信息
		if (null != nutritionArray && nutritionArray.size() > 0) {
			for (FruitNutritionEntity nutrition : nutritionArray) {
				nutrition.setFruitId(fruitEntity.getFruitId());
			}
			
			fruitNutritionWriter.batchInsertNutritionRelation(nutritionArray);
		}
		
		// 保存水果动态参数信息
		if (null != argumentArray && argumentArray.size() >= 0) {
			for (FruitArgumentEntity argument : argumentArray) {
				argument.setFruitId(fruitEntity.getFruitId());
			}
			
			fruitArgumentWriter.batchSave(argumentArray);
		}
		
		// 保存水果结构信息
		if (null != structureArray && structureArray.size() > 0) {
			for (FruitStructureEntity struct : structureArray) {
				struct.setFruitId(fruitEntity.getFruitId());
			}
			fruitStructureWriter.addBatchFruitStructureInfo(structureArray);
			
			for (FruitStructureEntity entity : structureArray) {
				if (null == entity.getEfficacy() || entity.getEfficacy().size() <= 0) {
					continue;
				}
				
				for (FruitEfficacyEntity efficacy : entity.getEfficacy()) {
					efficacy.setStructureId(entity.getSiteId());
					efficacy.setFruitId(fruitEntity.getFruitId());
					if (null == efficacy.getId() || efficacy.getFruitId().length() <= 0) {
						fruitEfficacyRelationWriter.save(efficacy);
					} else {
						fruitEfficacyRelationWriter.update(efficacy);
					}
				}
				
			}
		}
	}

}
